package com.woniuxy.car.commodity.service.core.controller;


import com.woniuxy.car.commodity.service.core.dto.CommodityClassDto;
import com.woniuxy.car.commodity.service.core.entity.CommodityClass;
import com.woniuxy.car.commodity.service.core.service.ICommodityClassService;
import com.woniuxy.common.api.CommonResult;
import com.woniuxy.common.api.enums.ResultCode;
import com.woniuxy.common.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author awz
 * @since 2022-03-01
 */
@Api(tags = "商品类别接口")
@AllArgsConstructor
@RestController
@RequestMapping("/commodity/commodity-class")
public class CommodityClassController {

    private ICommodityClassService commodityClassService;

    @ApiModelProperty("查询所有商品类别")
    @GetMapping
    public CommonResult<List<CommodityClass>> commodity_class(){
        List<CommodityClass> commodityClasses = commodityClassService.findCommodityClasses();
        return CommonResult.success(commodityClasses);
    }
    @ApiModelProperty("添加商品类别")
    @ApiImplicitParam(name = "commodityClassDto", value = "商品类别dto对象")
    @PostMapping
    public CommonResult<Boolean> addCommodityClass(@RequestBody CommodityClassDto commodityClassDto){

        try {
            commodityClassService.addCommodityClass(commodityClassDto);
        } catch (BusinessException e) {
            return CommonResult.failure(ResultCode.PARAM_IS_BLANK.getCode(), e.getMessage());
        }

        return CommonResult.success(true);

    }

}
